package cn.myspringboot.param.controller;

import cn.myspringboot.anno.PreAuthorize;
import cn.myspringboot.param.service.ParamService;
import cn.myspringboot.user.domain.Param;
import cn.myspringboot.user.query.ParamQuery;
import cn.myspringboot.utils.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@Api("参数相关接口")
@RequestMapping("/param")
public class ParamController {
    @Autowired
    private ParamService paramService;

    @PreAuthorize(name = "添加或修改参数信息",sn = "param:add",descs ="添加或修改参数信息")
    @ApiOperation("添加或修改参数信息")
    @PostMapping
    public R insert(@RequestBody Param param) {
        if(param.getId() == null){
            param.setCreateTime(new Date());
            param.setUpdateTime(new Date());
            paramService.add(param);
        }else {
            param.setUpdateTime(new Date());
            paramService.update(param);
        }
        return R.ok();
    }
    @PreAuthorize(name = "根据id删除参数信息",sn = "param:delete",descs ="根据id删除参数信息")
    @ApiOperation("根据id删除参数信息")
    @DeleteMapping("/{id}")
    public R delete(@PathVariable("id") Long id) {
        paramService.delete(id);
        return R.ok();
    }
    @PreAuthorize(name = "批量删除参数信息",sn = "param:delBatch",descs ="批量删除参数信息")
    @ApiOperation("批量删除参数信息")
    @PostMapping("/delBatch")
    public R delBatch(@RequestBody List<Long> ids) {
        paramService.batchDelete(ids);
        return R.ok();
    }
    @PreAuthorize(name = "根据id查询参数信息",sn = "param:getById",descs ="根据id查询参数信息")
    @ApiOperation("根据id查询参数信息")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(paramService.getById(id));
    }
    @PreAuthorize(name = "查询所有参数信息",sn = "param:getAll",descs ="查询所有参数信息")
    @ApiOperation("查询所有参数信息")
    @GetMapping("/getAll")
    public R getAll() {
        return R.ok(paramService.getAll());
    }
    @PreAuthorize(name = "分页查询参数信息",sn = "param:page",descs ="分页查询参数信息")
    @ApiOperation("分页查询参数信息")
    @PostMapping("/page")
    public R page(@RequestBody ParamQuery paramQuery) {
        PageHelper.startPage(paramQuery.getPage(),paramQuery.getSize());
        List<Param> params = paramService.queryPage(paramQuery);
        PageInfo<Param> pageInfo = new PageInfo<>(params);
        return R.ok(pageInfo);
    }
}
